#!/bin/sh

. scripts/functions

#
# Check to use multiple fileset include blocks.
# Verify by using different compression per block.
#

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=susan-fd
JobName=backup-susan-fd
Storage=File1
VolumeName=TestVolume001
DataDirs="1 2 3 4"

# Directory to backup.
BackupDirectory="${tmp}/data"

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

for i in $DataDirs; do
    mkdir -p ${tmp}/data/data$i
    echo "test" > ${tmp}/data/data$i/test.dat
done

# start the test
start_test

# ${tmp}/bconcmds lists the bconsole commands executed by "run_susan"
cat <<END_OF_DATA >${tmp}/bconcmds
messages
show fileset=FS_TESTJOB
@$out ${tmp}/log1.out w
label storage=${Storage} volume=${VolumeName} pool=Default
run job=$JobName storage=${Storage} yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} where=${tmp}/susan-restores select storage=${Storage}
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

# Start the susan daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_susan

# verify that all are terminated
check_for_zombie_jobs storage=File1 client=${Client}

# stop susan
stop_susan

# check tmp/log1.out and tmp/log2.out for errors
check_two_logs

# check for differences between original files and restored files
# gives an error, because top-level data directory is not backed up
# (and has therefore other permissions)
for i in $DataDirs; do
   check_restore_diff ${BackupDirectory}/data$i
done

# do some manual testing
if ! [ -d ${BackupDirectory} ]; then
    set_error "Directory ${BackupDirectory} does not exists any more."
fi


check_compression "${Storage}" "${VolumeName}" "1" "${tmp}/data/data2/test.dat" "gzip"
check_compression "${Storage}" "${VolumeName}" "1" "${tmp}/data/data3/test.dat" "gzip" "9"
# Warning: LZ4 compression support requested in fileset but not available on this platform. Disabling ...
#check_compression "${Storage}" "${VolumeName}" "1" "${tmp}/data/data3/test.dat" "LZ4"

# end tests and check for error codes
end_test

